package com.cxs.shopstore.service.impl;

import com.cxs.shopstore.es.ESTemplete;
import com.cxs.shopstore.es.ESgoodsDoc;
import com.cxs.shopstore.handler.JsoupHandler;
import com.cxs.shopstore.models.Good;
import com.cxs.shopstore.service.GoodDataPullService;
import com.cxs.shopstore.utils.DataCatch;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/5/7  21:41
 * @Version 1.0
 * */
@Slf4j
@Service
public class GoodDataPullServiceImpl implements GoodDataPullService {
    @Autowired
    private DataCatch dataCatch;
    @Autowired
    private JsoupHandler jsoupHandler;
    @Autowired
    public ESTemplete esTemplete;

    private String dataHtml;
    List<Good> list = new ArrayList<>();

    /**
     * 业务方法 京东抓取数据同步es服务器
     * @param keyword
     * @param page
     */
    @Override
    public void dataoutputes(String keyword,int page) {
        try {
            dataHtml = dataCatch.catchDataGet(keyword, page);
            list = jsoupHandler.dataHandle(dataHtml);
            List<ESgoodsDoc> eSDocs = esTemplete.changeList(list);
            esTemplete.bluksave(eSDocs);
        } catch (Exception e) {
            log.error("",e);
            e.printStackTrace();
        }
    }
    //解析爬去的网页为list<Good>
    @Override
    public List<Good> catchDataToList(String keyword, int page) {
        List<Good> list = new ArrayList<>();
        try {
            String dataHtml = dataCatch.catchDataGet(keyword, page);
            list = jsoupHandler.dataHandle(dataHtml);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
